//
//  AllDiscountViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/16.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "AllDiscountViewController.h"
#import "CountryModel.h"
#import "DiscountCell.h"
#import "DisCountWebView.h"

@interface AllDiscountViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
@property (nonatomic, strong) UITableView *disCountTab;
@property (nonatomic, strong) NSMutableArray *disCountArr;
@property (nonatomic, strong) NSMutableArray *localArr;
@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger pagg;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation AllDiscountViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)creatData {
    self.page++;
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863151025525517&track_app_version=6.7.1&track_app_channel=baidu&track_device_info=mx3&track_os=Android4.4.4&app_installtime=1444613810999&lat=38.876204&lon=121.560561&type=%@&id=%@&count=10&page=%ld&product_type=1016,1018,1020&time=1&order=2",self.type,self.allDiscount,self.page];
    [ZSMAFNetWork geturl:str body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        for (NSDictionary *dic in arr) {
        CountryModel *disModel = [[CountryModel alloc]initWithDic:dic];
            [self.disCountArr addObject:disModel];
        }
        [self.disCountTab reloadData];
        [self.disCountTab.footer endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    self.pagg++;
    NSString *lostr = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863151025525517&track_app_version=6.7.1&track_app_channel=baidu&track_device_info=mx3&track_os=Android4.4.4&app_installtime=1444613810999&lat=38.876204&lon=121.560561&type=%@&id=%@&count=10&page=%ld&product_type=2410&time=1&order=2",self.type,self.allDiscount,self.pagg];
    [ZSMAFNetWork geturl:lostr body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        for (NSDictionary *dic in arr) {
            CountryModel *locaModel = [[CountryModel alloc]initWithDic:dic];
            [self.localArr addObject:locaModel];
        }
        [self.disCountTab reloadData];
        self.hud.hidden = YES;
    } failure:^(NSError *error) {
        
    }];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
    
    self.disCountArr = [NSMutableArray array];
    self.localArr = [NSMutableArray array];
    [self creatData];
    [self creatView];
    // Do any additional setup after loading the view.
    // 左上角返回图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = [NSString stringWithFormat:@"%@特价折扣",self.placeName];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;

}

// 返回
- (void)leftAction:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)creatView {
    self.disCountTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 667) style:UITableViewStylePlain];
    [self.view addSubview:self.disCountTab];
    self.disCountTab.delegate = self;
    self.disCountTab.dataSource = self;
    self.disCountTab.rowHeight = HEIGHT * 100;
    self.hud = [MBProgressHUD showHUDAddedTo:self.disCountTab animated:YES];

    [self.hud hide:YES afterDelay:2];
    self.disCountTab.emptyDataSetDelegate = self;
    self.disCountTab.emptyDataSetSource = self;
    
    [self.disCountTab mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.right.top.bottom.equalTo(self.view);
    }];

    NSArray *arr = @[@"超值自由行",@"精彩当地游"];
    self.seg = [[UISegmentedControl alloc]initWithItems:arr];
    self.seg.selectedSegmentIndex = self.select;
    self.disCountTab.tableHeaderView = self.seg;
    [self.seg addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    
    [self createMJRefresh];
}
// 空白页占位图
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.view.bounds.size.height/2, WIDTH*375, 30)];
    label.text = @"! 网速不给力";
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = [UIColor redColor];
    return label;
}

- (void)createMJRefresh {
    // 上拉加载
    self.disCountTab.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(creatData)];
}

- (void)changePage:(UISegmentedControl *)seg {
    
    //每点1下刷新
    [self.disCountTab reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.seg.selectedSegmentIndex == 0) {
         return self.disCountArr.count;
    }else {
        return self.localArr.count;
    }
    
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"discouCell";
    DiscountCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[DiscountCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    
    if (self.seg.selectedSegmentIndex == 0) {
        CountryModel *disModel = self.disCountArr[indexPath.row];
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:disModel.photo] placeholderImage:[UIImage imageNamed:@"F"]];
        cell.leftLabel.text = disModel.priceoff;
        cell.topLabel.text = disModel.title;
        NSString *str = [[disModel.price stringByReplacingOccurrencesOfString:@"<em>" withString:@""]stringByReplacingOccurrencesOfString:@"</em>元起" withString:@""];
        cell.numberLabel.text = str;
        cell.rightLabel.text = @"元起";

    }else {
        CountryModel *locModel = self.localArr[indexPath.row];
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:locModel.photo] placeholderImage:[UIImage imageNamed:@"F"]];
        cell.leftLabel.text = locModel.priceoff;
        cell.topLabel.text = locModel.title;
        NSString *str = [[locModel.price stringByReplacingOccurrencesOfString:@"<em>" withString:@""]stringByReplacingOccurrencesOfString:@"</em>元起" withString:@""];
        cell.numberLabel.text = str;
        cell.rightLabel.text = @"元起";
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       [self.disCountTab deselectRowAtIndexPath:indexPath animated:YES];
    DisCountWebView *web = [[DisCountWebView alloc]init];
    if (self.seg.selectedSegmentIndex == 0) {
        CountryModel *model = self.disCountArr[indexPath.row];
        NSString *str = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.6&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_user_id=6654242%%20",model.uId];
        web.disCountStr = str;
    }else if (self.seg.selectedSegmentIndex == 1){
        CountryModel *local = self.localArr[indexPath.row];
        NSString *str = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.6&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_user_id=6654242%%20",local.uId];
        web.localStr = str;
    }
    [self.navigationController pushViewController:web animated:YES];
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.disCountTab.backgroundColor = [UIColor blackColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else {
        self.disCountTab.backgroundColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
